package com.ruoyi.zsyh.controller;

import java.util.List;

import com.ruoyi.common.core.domain.ZtreeString;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zsyh.domain.ZsyhServiceType;
import com.ruoyi.zsyh.service.IZsyhServiceTypeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;

/**
 * 服务分类Controller
 * 
 * @author yuexl
 * @date 2022-02-10
 */
@Controller
@RequestMapping("/zsyh/serviceType")
public class ZsyhServiceTypeController extends BaseController
{
    private String prefix = "zsyh/serviceType";

    @Autowired
    private IZsyhServiceTypeService zsyhServiceTypeService;

    @RequiresPermissions("zsyh:serviceType:view")
    @GetMapping()
    public String serviceType()
    {
        return prefix + "/serviceType";
    }

    /**
     * 查询服务分类树列表
     */
    @RequiresPermissions("zsyh:serviceType:list")
    @PostMapping("/list")
    @ResponseBody
    public List<ZsyhServiceType> list(ZsyhServiceType zsyhServiceType)
    {
        List<ZsyhServiceType> list = zsyhServiceTypeService.selectZsyhServiceTypeList(zsyhServiceType);
        return list;
    }

    /**
     * 导出服务分类列表
     */
    @RequiresPermissions("zsyh:serviceType:export")
    @Log(title = "服务分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZsyhServiceType zsyhServiceType)
    {
        List<ZsyhServiceType> list = zsyhServiceTypeService.selectZsyhServiceTypeList(zsyhServiceType);
        ExcelUtil<ZsyhServiceType> util = new ExcelUtil<ZsyhServiceType>(ZsyhServiceType.class);
        return util.exportExcel(list, "服务分类数据");
    }

    /**
     * 新增服务分类
     */
    @GetMapping(value = { "/add/{id}", "/add/" })
    public String add(@PathVariable(value = "id", required = false) String id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("zsyhServiceType", zsyhServiceTypeService.selectZsyhServiceTypeById(id));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存服务分类
     */
    @RequiresPermissions("zsyh:serviceType:add")
    @Log(title = "服务分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZsyhServiceType zsyhServiceType)
    {
        //设置级别
        if("0".equals(zsyhServiceType.getParentId())){
            zsyhServiceType.setServiceLevel("1");
        }else{
            ZsyhServiceType parentType = zsyhServiceTypeService.selectZsyhServiceTypeById(zsyhServiceType.getParentId());
            if("0".equals(parentType.getParentId())){
                zsyhServiceType.setServiceLevel("2");
            }else{
                zsyhServiceType.setServiceLevel("3");
            }
        }
        return toAjax(zsyhServiceTypeService.insertZsyhServiceType(zsyhServiceType));
    }

    /**
     * 修改服务分类
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        ZsyhServiceType zsyhServiceType = zsyhServiceTypeService.selectZsyhServiceTypeById(id);
        mmap.put("zsyhServiceType", zsyhServiceType);
        return prefix + "/edit";
    }

    /**
     * 修改保存服务分类
     */
    @RequiresPermissions("zsyh:serviceType:edit")
    @Log(title = "服务分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZsyhServiceType zsyhServiceType)
    {
        //设置级别
        if("0".equals(zsyhServiceType.getParentId())){
            zsyhServiceType.setServiceLevel("1");
        }else{
            ZsyhServiceType parentType = zsyhServiceTypeService.selectZsyhServiceTypeById(zsyhServiceType.getParentId());
            if("0".equals(parentType.getParentId())){
                zsyhServiceType.setServiceLevel("2");
            }else{
                zsyhServiceType.setServiceLevel("3");
            }
        }
        return toAjax(zsyhServiceTypeService.updateZsyhServiceType(zsyhServiceType));
    }

    /**
     * 删除
     */
    @RequiresPermissions("zsyh:serviceType:remove")
    @Log(title = "服务分类", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") String id)
    {
        //判断有下级的话不能删除
        ZsyhServiceType zsyhServiceType = new ZsyhServiceType();
        zsyhServiceType.setParentId(id);
        List<ZsyhServiceType> zsyhServiceTypeList = zsyhServiceTypeService.selectZsyhServiceTypeList(zsyhServiceType);
        if(zsyhServiceTypeList.size()>0){
            return error("该分类存在下级分类，不能删除!");
        }
        return toAjax(zsyhServiceTypeService.deleteZsyhServiceTypeById(id));
    }

    /**
     * 选择服务分类树
     */
    @GetMapping(value = { "/selectServiceTypeTree/{id}", "/selectServiceTypeTree/" })
    public String selectServiceTypeTree(@PathVariable(value = "id", required = false) String id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("zsyhServiceType", zsyhServiceTypeService.selectZsyhServiceTypeById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 选择服务分类树
     */
    @GetMapping(value = { "/selectServiceType/{id}", "/selectServiceType/" })
    public String selectServiceType(@PathVariable(value = "id", required = false) String id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("zsyhServiceType", zsyhServiceTypeService.selectZsyhServiceTypeById(id));
        }
        return prefix + "/selectServiceType";
    }

    /**
     * 加载服务分类树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<ZtreeString> treeData()
    {
        List<ZtreeString> ztrees = zsyhServiceTypeService.selectZsyhServiceTypeTree();
        return ztrees;
    }

    /**
     * 加载服务分类树列表
     */
    @GetMapping("/treeDataSecond")
    @ResponseBody
    public List<ZtreeString> treeDataSecond()
    {
        List<ZtreeString> ztrees = zsyhServiceTypeService.selectZsyhServiceTypeTreeSecond();
        return ztrees;
    }

    /**
     * 修改服务分类
     */
    @GetMapping("/editNext/{id}")
    public String editNext(@PathVariable("id") String id, ModelMap mmap)
    {
        ZsyhServiceType zsyhServiceType = zsyhServiceTypeService.selectZsyhServiceTypeById(id);
        mmap.put("zsyhServiceType", zsyhServiceType);
        return prefix + "/editNext";
    }

    /**
     * 新增服务分类
     *//*
    @GetMapping("/addNext")
    public String addNext(ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("zsyhServiceType", zsyhServiceTypeService.selectZsyhServiceTypeById(id));
        }
        return prefix + "/add";
    }*/
}
